#coding=utf-8
#此脚本全部为函数，方便直接调用进行自动化测试
from selenium import webdriver
import time, datetime
import os
import sys
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
import pytesseract
from PIL import Image, ImageEnhance

def login(username, password):
    # 定义浏览器及地址
    reload(sys)
    sys.setdefaultencoding('utf-8')
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(8)
    driver.get("http://op.yidai.xyz/")
    # 进行登录co环境的运营平台
    driver.find_element_by_id("adminname").send_keys(username)
    driver.find_element_by_id("password").send_keys(password)
    now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))  # 标的编号命名格式
    driver.save_screenshot('D:\python-photo\image')  # 截取当前网页，该网页有我们需要的验证码
    imgelement = driver.find_element_by_xpath('//img[@src="rand!loginRand.action"]')  # 定位验证码
    location = imgelement.location  # 获取验证码x,y轴坐标
    size = imgelement.size  # 获取验证码的长宽
    rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']),
              int(location['y'] + size['height']))  # 写成我们需要截取的位置坐标
    i = Image.open("D:\python-photo\image")  # 打开截图
    frame4 = i.crop(rangle)  # 使用Image的crop函数，从截图中再次截取我们需要的区域
    frame4.save('f://frame4.jpg')
    QQ = Image.open('f://frame4.jpg')
    text = pytesseract.image_to_string(qq).strip()  # 使用image_to_string识别验证码
    print text

#xyz环境发房易贷
def xyz_fang(url, adminname, password, borrowtype, borrowtitle, money, rate,username_type, borrowname,borrowmode, month, huankuanfangshi, cashgroup, agentname, rewardstate, rewardmode, reward):
    #op_Fdl(运营平台账号, 运营平台密码, 借款金额, 借款年利率, 借款人, P2P登录账号, P2P登录密码, P2P交易密码, 满标复审日期)
    # 定义浏览器及地址
    reload(sys)
    sys.setdefaultencoding('utf-8')
    #driverOptions = webdriver.ChromeOptions()
    #driverOptions.add_argument(r"user-data-dir=C:\Users\56443\AppData\Local\Google\Chrome\User Data\Default")
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(8)
    driver.get(url)
    # 进行登录co环境的运营平台
    driver.find_element_by_id("adminname").send_keys(adminname)
    driver.find_element_by_id("password").send_keys(password)
    driver.find_element_by_id("authCode").send_keys(u"123456")
    driver.find_element_by_xpath(".//*[@id='submitFrom']/ul/li[4]/input").click()
    # 判断是否登录成功，并输出至日志登录账号名称
    try:
        driver.find_element_by_xpath(".//*[@id='header']/section[1]/div/aside/em")
        a = True
    except:
        a = False
    if a == True:
        username = driver.find_element_by_xpath(".//*[@id='header']/section[1]/div/aside/em").text
        os.chdir('D:\python-photo\log')  # 切换到当前目录
        report = open("OP-login.doc", "a")  # 打开报告
        current_time = datetime.datetime.now()
        report.write("\n%s:" % current_time)
        report.write("开始发标，登录成功,账号昵称是：" + username + "\n")
        driver.find_element_by_xpath(".//*[@id='header']/section[2]/nav/ul/li[8]/a").click()
        driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[2]/a").click()
        #第一个页面
        now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))  # 标的编号命名格式,也为业务编号
        driver.find_element_by_xpath(".//*[@id='form1']/ul[1]/li[1]/aside/label[" + borrowtype +"]/input").click()
        driver.find_element_by_xpath(".//*[@id='form1']/ul[1]/li[2]/aside/input").send_keys(now)
        driver.find_element_by_xpath(".//*[@id='form1']/ul[1]/li[3]/aside/input").send_keys(now)
        driver.find_element_by_xpath(".//*[@id='form1']/ul[1]/li[4]/aside/input").clear()
        driver.find_element_by_xpath(".//*[@id='form1']/ul[1]/li[4]/aside/input").send_keys(borrowtitle + now)
        Select(driver.find_element_by_id("username_type")).select_by_visible_text(username_type)
        driver.find_element_by_id("username").send_keys(borrowname)

        if username_type == u"机构":
            Select(driver.find_element_by_id("borrow_use")).select_by_visible_text(u"临时周转")
            driver.find_element_by_id("account").send_keys(money)
            driver.find_element_by_xpath(
                ".//*[@id='form1']/ul[1]/li[8]/aside/label[" + borrowmode + "]/input").click()  # 选择为天标或者为月标,1为天标，2为月标
            driver.find_element_by_id("borrow_period").send_keys(month)
            driver.find_element_by_xpath(".//*[@id='borrow_style_box']/label[" + huankuanfangshi + "]/input").click()
            time.sleep(2)
            driver.find_element_by_id("borrow_apr").clear()
            driver.find_element_by_id("borrow_apr").send_keys(rate)
            Select(driver.find_element_by_id("cashgroup")).select_by_visible_text(cashgroup)
            driver.find_element_by_id("agent_username").send_keys(agentname)
            driver.find_element_by_xpath(".//*[@id='form1']/ul[3]/li[2]/aside/a").click()
            #第二个页面
            #driver.find_element_by_xpath(".//*[@id='form1']/ul/li[6]/aside/input").send_keys(u"陈强270496")
            driver.find_element_by_id("account_use").send_keys(u"借钱来耍耍")
            driver.find_element_by_link_text(u"下一步").click()
        if username_type == u"个人":
            Select(driver.find_element_by_id("borrow_use")).select_by_visible_text(u"临时周转")
            driver.find_element_by_id("account").send_keys(money)
            driver.find_element_by_xpath(
                ".//*[@id='form1']/ul[1]/li[8]/aside/label[" + borrowmode + "]/input").click()  # 选择为天标或者为月标,1为天标，2为月标
            driver.find_element_by_id("borrow_period").send_keys(month)
            driver.find_element_by_xpath(".//*[@id='borrow_style_box']/label[" + huankuanfangshi + "]/input").click()
            time.sleep(2)
            driver.find_element_by_id("borrow_apr").clear()
            driver.find_element_by_id("borrow_apr").send_keys(rate)
            Select(driver.find_element_by_id("cashgroup")).select_by_visible_text(cashgroup)
            driver.find_element_by_id("agent_username").send_keys(agentname)
            driver.find_element_by_xpath(".//*[@id='form1']/ul[3]/li[2]/aside/a").click()
            #第二个页面
            Select(driver.find_element_by_id("edu")).select_by_visible_text(u"本科")
            driver.find_element_by_xpath(".//*[@id='form1']/ul[2]/li[6]/aside/label[1]/input").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul[2]/li[7]/aside/input").send_keys(u"四川省南充市")
            driver.find_element_by_xpath(".//*[@id='form1']/ul[2]/li[8]/aside/input").send_keys(u"四川省成都市青羊区")
            driver.find_element_by_id("account_use").send_keys(u"借钱来耍耍")
            Select(driver.find_element_by_id("province")).select_by_visible_text(u"四川省")
            Select(driver.find_element_by_id("city")).select_by_visible_text(u"成都")
            driver.find_element_by_id("work_info").send_keys(u"工作很好，工资很高")
            driver.find_element_by_xpath(".//*[@id='form1']/ul[3]/li[7]/aside/span[1]/a").click()
        # 进入发标第三个页面
        # 切换iframe
        driver.switch_to.frame("iframebox1")
        Select(driver.find_element_by_xpath(".//*[@id='house_type']")).select_by_visible_text(u"商品房")
        #driver.find_element_by_xpath(".//*[@id='house']/li[3]/aside/label[1]/input").click()
        driver.find_element_by_xpath(".//*[@id='house']/li[5]/aside/input").send_keys(u"2015-07-25")
        driver.find_element_by_xpath(".//*[@id='house']/li[6]/aside/input").send_keys(u"100")
        driver.find_element_by_xpath(".//*[@id='house']/li[7]/aside/input").send_keys(u"成都市青羊区宽窄巷子")
        driver.find_element_by_xpath(".//*[@id='house']/li[8]/aside/input").send_keys(u"成都市公证处")
        driver.find_element_by_id("house_money").send_keys(u"100")
        driver.find_element_by_id("house_area").send_keys(u"100")
        driver.find_element_by_id("house_address").send_keys(u"成都市天府广场" + now)
        driver.find_element_by_id("house_byuser").send_keys(u"陈强")
        Select(driver.find_element_by_id("house_byuser_type")).select_by_visible_text(u"单独所有")
        driver.find_element_by_xpath(".//*[@id='house']/li[14]/aside/input").send_keys(now)
        driver.switch_to.default_content()
        driver.find_element_by_xpath(".//*[@id='form1']/ul[2]/li/aside/input[5]").click()
        driver.switch_to_alert().accept()
        # 进入发标第四个页面,并判断是否有奖励标
        driver.find_element_by_xpath(".//*[@id='house']/li[1]/aside/label["+ rewardstate +"]/input").click()
        #rewardstate为1时为无奖励
        if rewardstate == "1":
            driver.find_element_by_xpath(".//*[@id='house']/li[5]/aside/input").click()
            # 切换iframe
            WebElementframe = driver.find_element_by_xpath("html/body/div/iframe")
            driver.switch_to.frame(WebElementframe)
            driver.find_element_by_id("dpTodayInput").click()
            driver.switch_to.default_content()
            #driver.find_element_by_id("tender_account_min").clear()
            #driver.find_element_by_id("tender_account_min").send_keys(u"500")
            driver.find_element_by_xpath(".//*[@id='house']/li[8]/aside/span[1]/a").click()
        # rewardstate为2时为有奖励
        if rewardstate == "2":
            #输入奖励比例
            driver.find_element_by_xpath(".//*[@id='status_2']/aside/input").send_keys(reward)
            driver.find_element_by_xpath(".//*[@id='house']/li[5]/aside/input").click()
            WebElementframe = driver.find_element_by_xpath("html/body/div/iframe")
            driver.switch_to.frame(WebElementframe)
            driver.find_element_by_id("dpTodayInput").click()
            driver.switch_to.default_content()
            #driver.find_element_by_id("tender_account_min").clear()
            #driver.find_element_by_id("tender_account_min").send_keys(u"500")
            driver.find_element_by_xpath(".//*[@id='house']/li[8]/aside/span[1]/a").click()
            '''
            driver.find_element_by_xpath(".//*[@id='status_type']/aside/label["+ rewardmode +"]/input").click()
            #rewardmode为1时为现金奖励
            
            if rewardmode == "1":
                driver.find_element_by_xpath(".//*[@id='status_1']/aside/input").send_keys(reward)
                driver.find_element_by_xpath(".//*[@id='house']/li[9]/aside/input").click()
                # 切换iframe
                WebElementframe = driver.find_element_by_xpath("html/body/div/iframe")
                driver.switch_to.frame(WebElementframe)
                driver.find_element_by_id("dpTodayInput").click()
                driver.switch_to.default_content()
            # rewardmode为2时为按投资金额比例奖励
            if rewardmode == "2":
                driver.find_element_by_xpath(".//*[@id='status_2']/aside/input").send_keys(reward)
                driver.find_element_by_xpath(".//*[@id='house']/li[9]/aside/input").click()
                # 切换iframe
                WebElementframe = driver.find_element_by_xpath("html/body/div/iframe")
                driver.switch_to.frame(WebElementframe)
                driver.find_element_by_id("dpTodayInput").click()
                driver.switch_to.default_content()
            '''

        #driver.find_element_by_xpath(".//*[@id='house']/li[9]/aside/input").send_keys(u"2017-09-05 10:40")
        #截图并保存
        #pic_path = 'D:\\python-photo\\image\\' + now + 'FDL-biao' + '.png'  # 截图保存路径
        #print pic_path
        #driver.save_screenshot(pic_path)  # 截图
        # 切换iframe

        # 判断是否发布标的成功
        #根据业务编号进行搜索标，并进行审核
        driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[3]/a").click()
        Select(driver.find_element_by_id("searchType")).select_by_visible_text(u"业务编号")
        driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/input").send_keys(now)
        driver.find_element_by_xpath(".//*[@id='form1']/ul/li[4]/a").click()

        #判断是否发布成功
        try:
            driver.find_element_by_xpath(".//*[@id='tabScroll']/table/tbody/tr/td/a[1]")
            b = True
        except:
            b = False
        if b == True:
            borrowid = driver.find_element_by_xpath(".//*[@id='tabScroll']/div/table/tbody/tr/td[1]").text
            print u"借款编号是：" + borrowid
            #进行第一次审核
            driver.find_element_by_xpath(".//*[@id='tabScroll']/table/tbody/tr/td/a[1]").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/aside/label[1]/input").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[2]/aside/textarea").send_keys(u"审核成功")
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[3]/aside/span[1]/a").click()
            #按照业务编号进行搜索标
            driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[3]/a").click()
            Select(driver.find_element_by_id("searchType")).select_by_visible_text(u"业务编号")
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/input").send_keys(now)
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[4]/a").click()
            #进行第二次审核
            driver.find_element_by_xpath(".//*[@id='tabScroll']/table/tbody/tr/td/a[1]").click()
            driver.find_element_by_xpath(".//*[@id='house']/li[8]/aside/span[1]/a").click()
            # 按照业务编号进行搜索标
            driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[3]/a").click()
            Select(driver.find_element_by_id("searchType")).select_by_visible_text(u"业务编号")
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/input").send_keys(now)
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[4]/a").click()
            #进行第三次审核
            driver.find_element_by_xpath(".//*[@id='tabScroll']/table/tbody/tr/td/a[1]").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/aside/label[1]/input").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[2]/aside/textarea").send_keys(u"审核成功")
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[3]/aside/span[1]/a").click()
            #初审完毕，执行发标计划任务，进入投资中的借款
            if url == 'http://op.yidai.xyz':
                driver.get("http://task.yidai.xyz/worthautoissue.task.php")
            if url == 'http://op.yipaiwang.co':
                driver.get("http://task.yipaiwang.co/worthautoissue.task.php")
            if url == 'http://op.yipaiwang.cc':
                driver.get("http://task.yipaiwang.cc/worthautoissue.task.php")
            driver.quit()
            return u"借款标题是：" + borrowtitle + now + borrowid + u",借款人是：" + borrowname  +u"，借款编号是：" + borrowid
        if b == False:
            driver.quit()
            return u"发布标的失败！"
    if a == False:
        driver.quit()
        return u"登录失败！账号或密码错误！"

#资管系统外测-cc
def cc_fang(adminname, password, borrowtitle, money, rate,username_type, borrowname,borrowmode, month,cashgroup, agentname, rewardstate, rewardmode, reward):
    #op_Fdl(运营平台账号, 运营平台密码, 借款金额, 借款年利率, 借款人, P2P登录账号, P2P登录密码, P2P交易密码, 满标复审日期)
    # 定义浏览器及地址
    reload(sys)
    sys.setdefaultencoding('utf-8')
    #driverOptions = webdriver.ChromeOptions()
    #driverOptions.add_argument(r"user-data-dir=C:\Users\56443\AppData\Local\Google\Chrome\User Data\Default")
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(8)
    driver.get("http://op.yipaiwang.cc")
    # 进行登录co环境的运营平台
    driver.find_element_by_id("adminname").send_keys(adminname)
    driver.find_element_by_id("password").send_keys(password)
    driver.find_element_by_id("authCode").send_keys(u"123456")
    driver.find_element_by_xpath(".//*[@id='submitFrom']/ul/li[4]/input").click()
    # 判断是否登录成功，并输出至日志登录账号名称
    try:
        driver.find_element_by_xpath(".//*[@id='header']/section[1]/div/aside/em")
        a = True
    except:
        a = False
    if a == True:
        username = driver.find_element_by_xpath(".//*[@id='header']/section[1]/div/aside/em").text
        os.chdir('D:\python-photo\log')  # 切换到当前目录
        report = open("OP-login.doc", "a")  # 打开报告
        current_time = datetime.datetime.now()
        report.write("\n%s:" % current_time)
        report.write("开始发标，登录成功,账号昵称是：" + username + "\n")
        driver.find_element_by_xpath(".//*[@id='header']/section[2]/nav/ul/li[5]/a").click()
        driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[2]/a").click()
        #第一个页面
        now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))  # 标的编号命名格式,也为业务编号
        driver.find_element_by_xpath(".//*[@id='form1']/ul[1]/li[2]/aside/input").send_keys(now)
        driver.find_element_by_xpath(".//*[@id='form1']/ul[1]/li[3]/aside/input").send_keys(now)
        driver.find_element_by_xpath(".//*[@id='form1']/ul[1]/li[4]/aside/input").send_keys(borrowtitle + now)
        Select(driver.find_element_by_id("username_type")).select_by_visible_text(username_type)
        driver.find_element_by_id("username").send_keys(borrowname)

        if username_type == u"机构":
            Select(driver.find_element_by_id("borrow_use")).select_by_visible_text(u"临时周转")
            driver.find_element_by_id("account").send_keys(money)
            driver.find_element_by_xpath(
                ".//*[@id='form1']/ul[1]/li[8]/aside/label[" + borrowmode + "]/input").click()  # 选择为天标或者为月标,1为天标，2为月标
            driver.find_element_by_id("borrow_period").send_keys(month)
            time.sleep(2)
            driver.find_element_by_id("borrow_apr").clear()
            driver.find_element_by_id("borrow_apr").send_keys(rate)
            Select(driver.find_element_by_id("cashgroup")).select_by_visible_text(cashgroup)
            driver.find_element_by_id("agent_username").send_keys(agentname)
            driver.find_element_by_xpath(".//*[@id='form1']/ul[3]/li[2]/aside/a").click()
            #第二个页面
            #driver.find_element_by_xpath(".//*[@id='form1']/ul/li[6]/aside/input").send_keys(u"陈强270496")
            driver.find_element_by_id("account_use").send_keys(u"借钱来耍耍")
            driver.find_element_by_link_text(u"下一步").click()
        if username_type == u"个人":
            Select(driver.find_element_by_id("borrow_use")).select_by_visible_text(u"临时周转")
            driver.find_element_by_id("account").send_keys(money)
            driver.find_element_by_xpath(
                ".//*[@id='form1']/ul[1]/li[8]/aside/label[" + borrowmode + "]/input").click()  # 选择为天标或者为月标,1为天标，2为月标
            driver.find_element_by_id("borrow_period").send_keys(month)
            time.sleep(2)
            driver.find_element_by_id("borrow_apr").clear()
            driver.find_element_by_id("borrow_apr").send_keys(rate)
            Select(driver.find_element_by_id("cashgroup")).select_by_visible_text(cashgroup)
            driver.find_element_by_id("agent_username").send_keys(agentname)
            driver.find_element_by_xpath(".//*[@id='form1']/ul[3]/li[2]/aside/a").click()
            #第二个页面
            Select(driver.find_element_by_id("edu")).select_by_visible_text(u"本科")
            driver.find_element_by_xpath(".//*[@id='form1']/ul[2]/li[6]/aside/label[1]/input").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul[2]/li[7]/aside/input").send_keys(u"四川省南充市")
            driver.find_element_by_xpath(".//*[@id='form1']/ul[2]/li[8]/aside/input").send_keys(u"四川省成都市青羊区")
            driver.find_element_by_id("account_use").send_keys(u"借钱来耍耍")
            Select(driver.find_element_by_id("province")).select_by_visible_text(u"四川省")
            Select(driver.find_element_by_id("city")).select_by_visible_text(u"成都")
            driver.find_element_by_id("work_info").send_keys(u"工作很好，工资很高")
            driver.find_element_by_xpath(".//*[@id='form1']/ul[3]/li[7]/aside/span[1]/a").click()
        # 进入发标第三个页面
        # 切换iframe
        driver.switch_to.frame("iframebox1")
        Select(driver.find_element_by_xpath(".//*[@id='house_type']")).select_by_visible_text(u"商品房")
        driver.find_element_by_xpath(".//*[@id='house']/li[3]/aside/label[1]/input").click()
        driver.find_element_by_xpath(".//*[@id='house']/li[5]/aside/input").send_keys(u"2015-07-25")
        driver.find_element_by_xpath(".//*[@id='house']/li[6]/aside/input").send_keys(u"100")
        driver.find_element_by_xpath(".//*[@id='house']/li[7]/aside/input").send_keys(u"成都市青羊区宽窄巷子")
        driver.find_element_by_xpath(".//*[@id='house']/li[8]/aside/input").send_keys(u"成都市公证处")
        driver.find_element_by_id("house_money").send_keys(u"100")
        driver.find_element_by_id("house_area").send_keys(u"100")
        driver.find_element_by_id("house_address").send_keys(u"成都市天府广场" + now)
        driver.find_element_by_id("house_byuser").send_keys(u"陈强")
        Select(driver.find_element_by_id("house_byuser_type")).select_by_visible_text(u"单独所有")
        driver.find_element_by_xpath(".//*[@id='house']/li[14]/aside/input").send_keys(now)
        driver.switch_to.default_content()
        driver.find_element_by_xpath(".//*[@id ='form1']/ul[2]/li/aside/input[5]").click()
        driver.switch_to_alert().accept()
        # 进入发标第四个页面,并判断是否有奖励标
        '''
        driver.find_element_by_xpath(".//*[@id='house']/li[2]/aside/label["+ rewardstate +"]/input").click()
        #rewardstate为1时为无奖励
        if rewardstate == "1":
            driver.find_element_by_xpath(".//*[@id='house']/li[9]/aside/input").click()
            # 切换iframe
            WebElementframe = driver.find_element_by_xpath("html/body/div/iframe")
            driver.switch_to.frame(WebElementframe)
            driver.find_element_by_id("dpTodayInput").click()
            driver.switch_to.default_content()
        # rewardstate为2时为有奖励
        if rewardstate == "2":
            driver.find_element_by_xpath(".//*[@id='status_type']/aside/label["+ rewardmode +"]/input").click()
            #rewardmode为1时为现金奖励
            if rewardmode == "1":
                driver.find_element_by_xpath(".//*[@id='status_1']/aside/input").send_keys(reward)
                driver.find_element_by_xpath(".//*[@id='house']/li[9]/aside/input").click()
                # 切换iframe
                WebElementframe = driver.find_element_by_xpath("html/body/div/iframe")
                driver.switch_to.frame(WebElementframe)
                driver.find_element_by_id("dpTodayInput").click()
                driver.switch_to.default_content()
            # rewardmode为2时为按投资金额比例奖励
            if rewardmode == "2":
                driver.find_element_by_xpath(".//*[@id='status_2']/aside/input").send_keys(reward)
                driver.find_element_by_xpath(".//*[@id='house']/li[9]/aside/input").click()
                # 切换iframe
                WebElementframe = driver.find_element_by_xpath("html/body/div/iframe")
                driver.switch_to.frame(WebElementframe)
                driver.find_element_by_id("dpTodayInput").click()
                driver.switch_to.default_content()
        '''
        #driver.find_element_by_xpath(".//*[@id='house']/li[9]/aside/input").send_keys(u"2017-09-05 10:40")
        #截图并保存
        #pic_path = 'D:\\python-photo\\image\\' + now + 'FDL-biao' + '.png'  # 截图保存路径
        #print pic_path
        #driver.save_screenshot(pic_path)  # 截图
        # 切换iframe
        driver.find_element_by_xpath(".//*[@id='house']/li[2]/aside/input").click()
        WebElementframe = driver.find_element_by_xpath("html/body/div/iframe")
        driver.switch_to.frame(WebElementframe)
        driver.find_element_by_id("dpTodayInput").click()
        driver.switch_to.default_content()
        driver.find_element_by_id("tender_account_min").clear()
        driver.find_element_by_id("tender_account_min").send_keys(u"100")
        driver.find_element_by_xpath(".//*[@id='house']/li[5]/aside/span[1]/a").click()
        # 判断是否发布标的成功
        #根据业务编号进行搜索标，并进行审核
        driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[3]/a").click()
        Select(driver.find_element_by_id("searchType")).select_by_visible_text(u"业务编号")
        driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/input").send_keys(now)
        driver.find_element_by_xpath(".//*[@id='form1']/ul/li[4]/a").click()

        #判断是否发布成功
        try:
            driver.find_element_by_xpath(".//*[@id='tabScroll']/table/tbody/tr/td/a[1]")
            b = True
        except:
            b = False
        if b == True:
            borrowid = driver.find_element_by_xpath(".//*[@id='tabScroll']/div/table/tbody/tr/td[1]").text
            print u"借款编号是：" + borrowid
            #进行第一次审核
            driver.find_element_by_xpath(".//*[@id='tabScroll']/table/tbody/tr/td/a[1]").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/aside/label[1]/input").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[2]/aside/textarea").send_keys(u"审核成功")
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[3]/aside/span[1]/a").click()
            #按照业务编号进行搜索标
            driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[3]/a").click()
            Select(driver.find_element_by_id("searchType")).select_by_visible_text(u"业务编号")
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/input").send_keys(now)
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[4]/a").click()
            #进行第二次审核
            driver.find_element_by_xpath(".//*[@id='tabScroll']/table/tbody/tr/td/a[1]").click()
            driver.find_element_by_xpath(".//*[@id='house']/li[5]/aside/span[1]/a").click()
            # 按照业务编号进行搜索标
            driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[3]/a").click()
            Select(driver.find_element_by_id("searchType")).select_by_visible_text(u"业务编号")
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/input").send_keys(now)
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[4]/a").click()
            #进行第三次审核
            driver.find_element_by_xpath(".//*[@id='tabScroll']/table/tbody/tr/td/a[1]").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[1]/aside/label[1]/input").click()
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[2]/aside/textarea").send_keys(u"审核成功")
            driver.find_element_by_xpath(".//*[@id='form1']/ul/li[3]/aside/span[1]/a").click()
            #初审完毕，执行发标计划任务，进入投资中的借款
            driver.get("http://task.yipaiwang.cc/worthautoissue.task.php")
            driver.quit()
            return u"借款标题是：" + borrowtitle + now + u",借款人是：" + borrowname  + u"，借款编号是：" + borrowid
        if b == False:
            driver.quit()
            return u"发布标的失败！"
    if a == False:
        driver.quit()
        return u"登录失败！账号或密码错误！"


def FaHongbao(adminname, op_password, p2p_username, p2p_password, hongbaoid, userid):
    # 定义浏览器及地址
    reload(sys)
    sys.setdefaultencoding('utf-8')
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(8)
    driver.get("http://op.yidai.xyz/")
    # 进行登录co环境的运营平台
    driver.find_element_by_id("adminname").send_keys(adminname)
    driver.find_element_by_id("password").send_keys(op_password)
    driver.find_element_by_id("authCode").send_keys(u"123456")
    driver.find_element_by_xpath(".//*[@id='submitFrom']/ul/li[4]/input").click()
    # 判断是否登录成功，并输出至日志登录账号名称
    try:
        driver.find_element_by_xpath(".//*[@id='header']/section[1]/div/aside/em")
        a = True
    except:
        a = False
    if a == True:
        #进入推广管理页面
        driver.find_element_by_xpath(".//*[@id='header']/section[2]/nav/ul/li[4]/a").click()
        #进入红包页面、手动发放页面
        driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[3]/a").click()
        #点击新增发放
        driver.find_element_by_xpath("html/body/main/section/div/div[1]/a").click()
        #选择为现金红包
        driver.find_element_by_xpath(".//*[@id='myTab']/li[2]/a").click()
        #按照红包序号进行搜索，须记住自己的红包序号
        driver.find_element_by_xpath(".//*[@id='searchFormId']/ul/li[1]/input[1]").send_keys(hongbaoid)
        driver.find_element_by_xpath(".//*[@id='searchFormId']/ul/li[1]/button").click()
        #判断是否有此红包，若有，进入下一步，若无，返回无此红包
        try:
            driver.find_element_by_xpath(".//*[@id='html-2']/tr/td[1]/button")
            b = True
        except:
            b = False
        if b == True:
            #点击添加按钮
            time.sleep(3)
            driver.find_element_by_xpath(".//*[@id='html-7']/tr[1]/td[1]/button").click()
            #text = driver.find_element_by_xpath(".//*[@id='tender']/table/tbody/tr/td[4]").text  # 获取此红包的金额大小
            #print text
            time.sleep(2)
            driver.find_element_by_id("cname").send_keys(u"P2P---陈强")
            #选择少量上传
            driver.find_element_by_id("upload-name").click()
            #输入用户在运营平台的ID号
            driver.find_element_by_id("text-username").send_keys(userid)
            driver.find_element_by_id("FormSubmit").click()
            #进入审核
            driver.find_element_by_xpath(".//*[@id='subnav']/dl/dd[5]/a").click()
            driver.find_element_by_xpath("html/body/main/section/div/div[1]/table/tbody/tr[1]/td[6]/a").click()
            driver.find_element_by_xpath(".//*[@id='submitForm']/div[1]/input[1]").click()
            driver.find_element_by_id("audit_remark").send_keys(u"审核成功！")
            driver.find_element_by_id("submitBtn").click()
            driver.quit()
            '''#进入P2P前台查看红包是否发放正确
            driver.get("http://www.yidai.xyz/user/login/")
            driver.find_element_by_id("keywords").send_keys(p2p_username)
            driver.find_element_by_id("password").send_keys(p2p_password)
            driver.find_element_by_id("login_button").click()
            #进入红包页面
            driver.find_element_by_xpath("html/body/div[2]/div[2]/dl[5]/dt/a").click()
            #判断是否红包发放成功，成功返回正确，失败返回发放红包失败
            try:
                driver.find_element_by_xpath("html/body/div[2]/div[3]/div/div[2]/div[2]/div[2]/div/div[2]/div/div[2]")
                c = True
            except:
                c = False
            if c == True:
                money = driver.find_element_by_xpath("html/body/div[2]/div[3]/div/div[2]/div[2]/div[2]/div/div[2]/div/div[2]").text
                money1 = money.replace(',','')
                print money1
                if money1 == text:
                    driver.quit()
                    return u"红包发放成功，金额为：" + money + "元！"

                else:
                    driver.quit()
                    return u"红包发放失败,金额不正确！"
            if c == False:
                driver.quit()
                return u"红包发放失败！"
            '''
        if b == False:
            driver.quit()
            return u"无此红包，请再次确认红包序号！"

    if a == False:
        driver.quit()
        return u"登录失败"